home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / pdfrw / pdfcompress.py < prev    next >
Encoding:
Python Source  |  2011-08-22  |  1.6 KB  |  58 lines

  1. # A part of pdfrw (pdfrw.googlecode.com)
  2. # Copyright (C) 2006-2009 Patrick Maupin, Austin, Texas
  3. # MIT license -- See LICENSE.txt for details
  4.  
  5. '''
  6. Currently, this sad little file only knows how to decompress
  7. using the flate (zlib) algorithm.  Maybe more later, but it's
  8. not a priority for me...
  9. '''
  10.  
  11. from __future__ import generators
  12.  
  13. try:
  14.     set
  15. except NameError:
  16.     from sets import Set as set
  17.  
  18. import zlib
  19. from pdfobjects import PdfDict, PdfName
  20.  
  21.  
  22. def streamobjects(mylist):
  23.     for obj in mylist:
  24.         if isinstance(obj, PdfDict) and obj.stream is not None:
  25.             yield obj
  26.  
  27. def uncompress(mylist, warnings=set()):
  28.     flate = PdfName.FlateDecode
  29.     for obj in streamobjects(mylist):
  30.         ftype = obj.Filter
  31.         if ftype is None:
  32.             continue
  33.         if isinstance(ftype, list) and len(ftype) == 1:
  34.             # todo: multiple filters
  35.             ftype = ftype[0]
  36.         parms = obj.DecodeParms
  37.         if ftype != flate or parms is not None:
  38.             msg = 'Not decompressing: cannot use filter %s with parameters %s' % (repr(ftype), repr(parms))
  39.             if msg not in warnings:
  40.                 warnings.add(msg)
  41.                 print msg
  42.         else:
  43.             obj.stream = zlib.decompress(obj.stream)
  44.             obj.Filter = None
  45.  
  46. def compress(mylist):
  47.     flate = PdfName.FlateDecode
  48.     for obj in streamobjects(mylist):
  49.         ftype = obj.Filter
  50.         if ftype is not None:
  51.             continue
  52.         oldstr = obj.stream
  53.         newstr = zlib.compress(oldstr)
  54.         if len(newstr) < len(oldstr) + 30:
  55.             obj.stream = newstr
  56.             obj.Filter = flate
  57.             obj.DecodeParms = None
  58.